﻿@model CustomerDownloadableProductsModel

@using SmartStore.Web.Models.Customer;

@{
	Html.AddTitleParts(T("PageTitle.Account").Text);

	Layout = "_MyAccount";
	ViewBag.PageClassToken = "downloads";
	ViewBag.PageTitle = T("Account.DownloadableProducts").Text;
}

@if (Model.Items.Count == 0)
{
	<div class="alert alert-warning">
		@T("DownloadableProducts.NoItems")
	</div>
	return;
}

<div class="card-deck card-cols-sm-1 esd-list">
	@foreach (var item in Model.Items)
	{
		<div class="card shadow-sm esd-item">
			<div class="card-body">
				<h4 class="card-title mb-4" @Html.LanguageAttributes(item.ProductName)>@item.ProductName.Value.Truncate(50, "...")</h4>
				<dl class="row mb-0">
					<dt class="col-sm-3 font-weight-400 text-muted">@T("Order.Order#")</dt>
					<dd class="col-sm-9"><a href="@Url.Action("Details", "Order", new { id = item.OrderId })" rel="nofollow">@item.OrderId</a></dd>
					<dt class="col-sm-3 font-weight-400 text-muted">@T("Common.CreatedOn")</dt>
					<dd class="col-sm-9">@item.CreatedOn.ToNativeString("d")</dd>
					<dt class="col-sm-3 font-weight-400 text-muted">@T("DownloadableProducts.Fields.Product")</dt>
					<dd class="col-sm-9">
						<a href="@item.ProductUrl" title="@T("Products.Details")" @Html.LanguageAttributes(item.ProductName)>@item.ProductName</a>
						@if (item.ProductAttributes.HasValue())
						{
							<div>
								@Html.Raw(item.ProductAttributes)
							</div>
						}
					</dd>
				</dl>
			</div>

			@if (item.IsDownloadAllowed || item.LicenseId > 0)
            {
                <div class="card-footer p-0">
                    <div class="d-flex">
                        @if (item.IsDownloadAllowed)
                        {
                            <a class="btn btn-secondary btn-flat rounded-0" href="@Url.Action("GetDownload", "Download", new { id = item.OrderItemGuid })" rel="nofollow">
                                <i class="fa fa-download"></i>
                                <span>@T("DownloadableProducts.Fields.Download")</span>
                            </a>

                            if (item.DownloadVersions.Count > 1)
                            {
                                <a class="btn btn-secondary btn-flat rounded-0" data-toggle="collapse" href="#download-versions-@item.OrderId" rel="nofollow">
                                    <i class="fa fa-info"></i>
                                    <span>@T("Common.Download.Versions")</span>
                                </a>
                            }
                        }
                        else
                        {
                            <a class="btn btn-secondary btn-flat rounded-0 disabled" href="javascript:void(0)" rel="nofollow">
                                <span>@T("DownloadableProducts.Fields.Download.NA")</span>
                            </a>
                        }

                        @if (item.LicenseId > 0)
                        {
                            <a class="btn btn-secondary btn-flat rounded-0" href="@Url.Action("GetLicense", "Download", new { id = item.OrderItemGuid })" rel="nofollow">
                                <span>@T("DownloadableProducts.Fields.DownloadLicense")</span>
                            </a>
                        }
                    </div>
                </div>

                if (item.IsDownloadAllowed && item.DownloadVersions.Count > 1)
                {
                    <div class="collapse download-versions" id="download-versions-@item.OrderId">
                        <table class="table mb-0">
                            <thead>
                                <tr>
                                    <th scope="col"></th>
                                    <th scope="col">#</th>
                                    <th scope="col">@T("Common.Download.Version")</th>
                                </tr>
                            </thead>
                            <tbody id="download-versions">
                                @foreach (var download in item.DownloadVersions)
                                {
                                    <tr data-toggle="collapse" data-target="#download-@download.DownloadId" class="download-version">
                                        <th scope="row">
                                            <i class="fa fa-fw fa-angle-right"></i>
                                        </th>
                                        <td>
                                            @download.FileVersion
                                        </td>
                                        <td>
                                            <a href="@Url.Action("GetDownload", "Download", new { id = item.OrderItemGuid, fileVersion = download.FileVersion })" class="download-link">
                                                @download.FileName
                                            </a>
                                        </td>
                                    </tr>
                                    
                                    <tr id="download-@download.DownloadId" class="download-changelog collapse bg-light">
                                        <td colspan="3">
                                            <div>
                                                @if (download.Changelog.HasValue())
                                                {
                                                    @Html.Raw(download.Changelog)
                                                }
                                                else
                                                {
                                                    @T("Customer.Downloads.NoChangelogAvailable")
                                                }
                                            </div>
                                        </td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                    </div>   
                }
            }
		</div>
	}
</div>

<script type="text/javascript">

	$(function () {
        $('.download-versions .download-changelog').on('show.bs.collapse hide.bs.collapse', function (e) {
            var icon = $(e.target).prev().find("i");
            icon.toggleClass("fa-angle-down", e.type == "show");
        });

        $('.download-versions .download-link').on("click", function (e) {
            e.stopPropagation();
        });
	});

</script>